import urllib2
from advanced_json_encoding import encode, decode

def retrieval_method(url):
    """Create a function used to retrieve and decode a url.
    
    Given a url, will return a function that can be called without arguments to
    request that url, decode the json results, and return the decoded results.
    """
    def m():
        results = urllib2.urlopen(url).read()
        return decode(results, key_names_to_strings=True)

    return m

def poster_method(base_url):
    """Create a function used to post to a url.

    Given a url, will return a function that can be called with two arguments:

    update_action: The "action" of the url (really, the end of the url string)
    data: The python data that should be sent (which will be encoded into json)
    """
    def m(update_action, data):
        url = base_url + update_action
        headers = {'Content-Type' : 'application/json'}
        json_data = encode(data)

        request = urllib2.Request(url, json_data, headers)

        urllib2.urlopen(request)

    return m

